package test;

import repast.simphony.parameter.Parameters;
import defectsim.SimulationEnvironment;
import defectsim.agents.DefectAgent;
import defectsim.agents.DefectFactory;
import defectsim.agents.EdgeDislocation;
import defectsim.agents.PointDefect;
import defectsim.agents.DefectFactory.DefectType;
import defectsim.material.SlipSystem;

public class DislocationPointSourceTester extends DefectAgent {
	
	private SimulationEnvironment env = SimulationEnvironment.getInstance();
	private double gridScale;

	public DislocationPointSourceTester(){
		Parameters p = env.getParameters();
		int gridSize = (Integer)p.getValue("gridsize");		
		double boxLength = (Double)p.getValue("spacelength");
		this.gridScale = gridSize / boxLength;
	}

	@Override
	public void percept() {
	}

	@Override
	public void mentalUpdate() {
	}

	@Override
	public void act() {
		if (env.getCurrentTick() ==  1.0) {
			PointDefect pd = (PointDefect) DefectFactory.createDefect(DefectType.PointDefect);
			pd.setX(3.0);
			pd.setY(5.0);
			pd.setPointDefectSize(1000*0.255);
			env.add(pd);
			this.multiply(1);
		}
		if (env.getCurrentTick() % 400 ==  0.0)
			this.multiply(1);
	}

	private void multiply(int noOfDislocations){
		int s = 0;
		SlipSystem[] slipsys = env.getMaterial().getSlipSystems();
		EdgeDislocation dislocation = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
		dislocation.setSlipSystem(slipsys[s]);
		dislocation.setUnitBurgersVector(-1);
		double dx = 8.0;
		double dy = 5.0;
		dislocation.setX(dx);
		dislocation.setY(dy);
		dislocation.setLocationGridX((int)(dx * gridScale));
		dislocation.setLocationGridY((int)(dy * gridScale));
		env.nucleate(dislocation);
	}

}
